<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Data_fetcher extends CI_Model {
	
	
	function fetch_table($table,$order=null,$limit=null,$where=null)
	{				
		
		if($order!=null)
			$this->db->order_by($order[0],$order[1]);
		elseif($table == 'tb_mas_content')
			$this->db->order_by('dteStart','desc');
		
		if($limit!=null)
			$this->db->limit($limit);
			
		if($where!=null)
			$this->db->where($where);
		
		$data =  $this->db
						->get($table)
						->result_array();
						
		return $data;
						
	}
	
    
	function fetch_single_data($table,$id=1)
	{
		$data = $this->db
					 ->get_where($table,array('intId'=>$id))
					 ->result_array();
		return current($data);
	}
	
	
	function count_results($table)
	{
		return $this->db->count_all($table);
	}
	
	function get_validation_items($table)
	{
		$data = $this->db
					 ->get_where('su-tb_sys_validation',array('strTable'=>$table))
					 ->result_array();
		return $data;
	}
	
	function get_home_bg()
    {
        $bg = $this->db->select('*')->where('fk-tb_sys_adspace',20)->get('tb_sys_advertising')->result_array();
        shuffle($bg);
        return current($bg);
        
    }
    
    public function logout_user($userid)
	{
		$user = $this->db->get_where('su-tb_sys_users',array('intID'=>$userid))->result_array();
		$user = current($user);
		
		//de-initialize user's session
		
		foreach($user as $key=>$value)
		{
			$this->session->unset_userdata($key);			
		}
		$this->session->unset_userdata('admin_logged_in');
        
	}

}